﻿using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wams.Admin
{
    public class WamsMessage
    {
        public string Name;
        protected List<string> Arguments = new List<string>();
        public override string ToString()
        {
            return string.Format("{0} {1}", Name, string.Join(" ", Arguments.Select(x => x.ToString()).ToArray()));
        }

        public static string GetQueueName(string containerName, string deploymentId, int instanceId)
        {
            return string.Format("{0}-{1}-{2}", containerName, deploymentId, instanceId).Replace("(", "").Replace(")", "").Replace("_","-").ToLower();
        }

        public void Send(string containerName, CloudStorageAccount storageAccount, string deploymentId, int instanceId)
        {
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            CloudQueue instanceQueue = queueClient.GetQueueReference(GetQueueName(containerName, deploymentId, instanceId));
            instanceQueue.AddMessage(new CloudQueueMessage(this.ToString()));
        }

        public static WamsMessage Parse(string msg)
        {
            WamsMessage command = null;
            string[] commandAndArguments = msg.Split(' ');
            
            switch (commandAndArguments[0].ToLower())
            {
                case "synccontent": command = WamsMessage.SyncContent;
                    break;
                case "syncsettings": command = WamsMessage.SyncSettings;
                    break;
                case "refreshresourcelist": command = WamsMessage.RefreshResourceList;
                    break;
                case "setbusystate": command = WamsMessage.SetBusyState;
                    break;
                case "ping": command = WamsMessage.Ping;
                    break;
                case "recycle": command = WamsMessage.Recycle;
                    break;
                case "synccertificates": command = WamsMessage.SyncCertificates; break;
                default: return null;
            }
            command.Arguments = commandAndArguments.ToList();
            command.Arguments.RemoveAt(0); // Removing the command name
            return command;
        }

        #region Quickhand commands
        public static SyncContentMessage SyncContent { get { return new SyncContentMessage(); } }
        public static SyncSettingsMessage SyncSettings { get { return new SyncSettingsMessage(); } }
        public static RefreshResourceListMessage RefreshResourceList { get { return new RefreshResourceListMessage(); } }
        public static SetBusyStateMessage SetBusyState { get { return new SetBusyStateMessage(); } }
        public static PingMessage Ping { get { return new PingMessage(); } }
        public static RecycleMessage Recycle { get { return new RecycleMessage(); } }
        public static SyncCertificatesMessage SyncCertificates { get { return new SyncCertificatesMessage(); } }
        #endregion
    }

    public class SyncContentMessage : WamsMessage
    {
        public int SiteId
        {
            get { return int.Parse(Arguments[0]); }
            set { Arguments[0] = value.ToString(); }
        }
        public SyncContentMessage()
        {
            Name = "synccontent";
            Arguments.Add("-1");
        }        
    }


    public class SyncSettingsMessage : WamsMessage
    {
        public int SiteId
        {
            get { return int.Parse(Arguments[0]); }
            set { Arguments[0] = value.ToString(); }
        }
        public SyncSettingsMessage()
        {
            Name = "syncsettings";
            Arguments.Add("-1");
        }
    }
    public class RefreshResourceListMessage : WamsMessage
    {
        public RefreshResourceListMessage()
        {
            Name = "refreshresourcelist";
        }
    }

    public class SetBusyStateMessage : WamsMessage
    {
        public bool IsBusy
        {
            get { return int.Parse(Arguments[0]) == 1 ? true: false; }
            set { Arguments[0] = value ? "1": "0"; }
        }
        public SetBusyStateMessage()
        {
            Name = "setbusystate";
            Arguments.Add("0");
        }
    }

    public class PingMessage : WamsMessage
    {
        public PingMessage()
        {
            Name = "ping";
        }
    }

    public class RecycleMessage : WamsMessage
    {
        public RecycleMessage()
        {
            Name = "recycle";
        }
    }

    public class SyncCertificatesMessage : WamsMessage
    {
        public SyncCertificatesMessage()
        {
            Name = "synccertificates";
        }
    }
}
